package com.example.controller;

import com.example.common.Result;
import com.example.entity.Banner;
import com.example.service.BannerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


//广告前端操作接口
@RestController
@RequestMapping("/banner")
public class BannerController {
    @Autowired
    private BannerService bannerService;

    //新增
    @PostMapping("/add")
    public Result add(@RequestBody Banner banner){
       bannerService.add(banner);
       return Result.success();
    }

    //删除
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable Integer id){
        bannerService.deleteById(id);
        return Result.success();
    }

    //批量删除
    @DeleteMapping("/delete/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids){
        bannerService.deleteBatch(ids);
        return Result.success();
    }

    //修改
    @PutMapping("/update")
    public Result updateById(@RequestBody Banner banner){
        bannerService.updateById(banner);
        return Result.success();
    }

    //根据id查询
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Banner banner = bannerService.selectById(id);
        return Result.success(banner);
    }

    //查询所有
    @GetMapping("/selectAll")
    public Result selectAll(Banner banner){
        List<Banner> list =  bannerService.selectAll(banner);
        return Result.success(list);
    }

    //分页查询
    @GetMapping("/selectPage")
    public Result selectPage(Banner banner,
                             @RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Banner> page = bannerService.selectPage(banner, pageNum, pageSize);
        return Result.success(page);
    }
}
